package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.constant.CstScheme;
import com.ouertech.android.agm.lib.base.utils.UtilDevice;
import com.ouertech.android.agm.lib.base.utils.UtilLog;
import com.ouertech.android.agm.lib.ui.base.BaseUIActivity;
import com.ouertech.android.agm.lib.ui.base.MenuTxt;
import com.ouertech.android.agm.lib.ui.base.UIOptions;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseFullActivity;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.ImageTag;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.ui.dialog.ImageTagEditDialog;
import com.simon.mengkou.ui.dialog.ImageTagPropertiesDialog;
import com.simon.mengkou.ui.view.label.AnimateLabelView;
import com.simon.mengkou.ui.view.label.LabelInfo;
import com.simon.mengkou.wxpay.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xiazhenshui on 16/6/20.
 * 图片标签编辑
 */

public class ImageTagEditActivity extends BaseUIActivity implements View.OnTouchListener{
    //图片
    @Bind(R.id.image_tag_id_image)
    SimpleDraweeView mSdvImage;
    //图片标签父布局
    @Bind(R.id.image_tag_id_tag_root)
    RelativeLayout mFlTagRoot;

    AnimateLabelView currLabel;
    AnimateLabelView editLabel;


    ImageTagEditDialog mEditDialog;

    private String mPath;
    private int mWidth;
    private ArrayList<ImageTag> mImageTags;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mPath = getIntent().getStringExtra(CstOuer.KEY.PATH);
        mWidth = UtilDevice.getDevice(this).getWidth();
        mEditDialog = new ImageTagEditDialog(this);
        mImageTags = new ArrayList<>();
    }

    @Override
    protected long initOptions() {
        return UIOptions.UI_OPTIONS_SYSTEMBAR
                | UIOptions.UI_OPTIONS_APPBAR_CUSTIOM
                | UIOptions.UI_OPTIONS_CONTENT_CUSTOM;
    }

    @Override
    protected void initAppCustom() {
        setAppCustomLayout(R.layout.layout_image_tag_edit_header);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_image_tag_edit);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        OuerApplication.mImageLoader.loadImage(mSdvImage, CstScheme.FILE + mPath);

        mFlTagRoot.getLayoutParams().height = mWidth;
        mFlTagRoot.setOnTouchListener(ImageTagEditActivity.this);



        //删除标签
        mEditDialog.setOnDeleteListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImageTags.remove(editLabel.getTag());
                editLabel.hide();
                mEditDialog.dismiss();
            }
        });


        //编辑标签
        mEditDialog.setOnEditListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageTagPropertiesDialog dialog = new ImageTagPropertiesDialog(ImageTagEditActivity.this);
                dialog.setOnFinishListener(new ImageTagPropertiesDialog.OnFinishListener() {
                    @Override
                    public void onFinish(ImageTag tag) {
                        editTag(tag);
                    }
                });
                dialog.show();

                dialog.setImageTag((ImageTag) editLabel.getTag());
                mEditDialog.dismiss();
            }
        });

    }


    float downX, downY;
    float labelOx, labelOy;
    long downTime;
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (currLabel != null) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    downTime = System.currentTimeMillis();
                    downX = event.getX();
                    downY = event.getY();

                    if (currLabel != null) {
                        labelOx = currLabel.pointAtX;
                        labelOy = currLabel.pointAtY;
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (currLabel != null) {
                        float x = labelOx + event.getX() - downX;
                        float y = labelOy + event.getY() - downY;
                        if(x > 40 && x < mWidth-40 && y > 40 && y < mWidth-40) {
                            currLabel.setPointAt(x, y);
                        }
                    }

                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    if (currLabel != null) {
                        float upX = event.getX();
                        float upY = event.getY();
                        double distance = Math.sqrt(Math.abs(upX - downX)
                                * Math.abs(upX - downX)
                                + Math.abs(upY - downY)
                                * Math.abs(upY - downY));//两点之间的距离
                        if (distance < 15) { // 距离较小，当作click事件来处理
                            if(System.currentTimeMillis() - downTime > 500) {
                                editLabel = currLabel;
                                mEditDialog.showAsDropDown(currLabel);
                            } else {
                                currLabel.onCenterClick();
                            }
                        }

                        ImageTag tag = (ImageTag) currLabel.getTag();
                        tag.setXratio(upX/mWidth);
                        tag.setYratio(upY/mWidth);
                        currLabel = null;
                    }

                    break;

            }

        } else {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    downX = event.getX();
                    downY = event.getY();
                    break;

                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    float upX = event.getX();
                    float upY = event.getY();
                    double distance = Math.sqrt(Math.abs(upX - downX)
                            * Math.abs(upX - downX)
                            + Math.abs(upY - downY)
                            * Math.abs(upY - downY));//两点之间的距离
                    if (distance < 15) { // 距离较小，当作click事件来处理
                        ImageTagPropertiesDialog dialog = new ImageTagPropertiesDialog(this);
                        dialog.setRatioXY(upX/mWidth, upY/mWidth);
                        dialog.setOnFinishListener(new ImageTagPropertiesDialog.OnFinishListener() {
                            @Override
                            public void onFinish(ImageTag tag) {
                                addTag(tag);
                            }
                        });
                        dialog.show();
                    }

                    break;
            }
        }

        return true;
    }


    /**
     * 返回
     */
    @OnClick(R.id.image_tag_id_nav)
    public void goNav() {
        finish();
    }

    /**
     * 完成
     */
    @OnClick(R.id.image_tag_id_finish)
    public void goFinish() {
        Intent intent = new Intent();
        intent.putExtra(CstOuer.KEY.PATH, mPath);
        intent.putExtra(CstOuer.KEY.TAGS, mImageTags);
        setResult(RESULT_OK, intent);
       finish();
    }


    private void editTag(ImageTag tag) {
        editLabel.setText1(tag.getBrand());
        editLabel.setText2(tag.getPrice());
        editLabel.setText3(tag.getLocation());

        ImageTag t = (ImageTag) editLabel.getTag();
        t.setBrand(tag.getBrand());
        t.setPrice(tag.getPrice());
        t.setLocation(tag.getLocation());
    }


    private void addTag(ImageTag tag) {
        mImageTags.add(tag);
        LabelInfo info = new LabelInfo();
        info.title1Text = tag.getBrand();
        info.title2Text = tag.getPrice();
        info.title3Text = tag.getLocation();
        info.pcX = tag.getXratio();
        info.pcY = tag.getYratio();
        AnimateLabelView label = new AnimateLabelView(ImageTagEditActivity.this);
        label.setLabelInfo(info);
        label.setTag(tag);
        label.alwaysWave();

        label.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    currLabel = (AnimateLabelView)v;
                }
                return false;
            }
        });

        mFlTagRoot.addView(label);
    }


}
